/*
 * Function test for parsing enveloped data using glib framework
 *
 * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */
#include <glib.h>
#include <stdint.h>
#include "cms.h"

/* Enveloped Data from KMS */
static uint8_t enveloped_data_der[] = {
    0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x03,
    0xa0, 0x80, 0x30, 0x80, 0x02, 0x01, 0x02, 0x31, 0x82, 0x01, 0x6b, 0x30, 0x82,
    0x01, 0x67, 0x02, 0x01, 0x02, 0x80, 0x20, 0x70, 0x87, 0xb5, 0x5d, 0xf9, 0x4b,
    0xa3, 0x25, 0x5c, 0xdf, 0x3c, 0x01, 0x84, 0x0a, 0x2a, 0xa3, 0xec, 0x93, 0x62,
    0x06, 0x25, 0xd6, 0x01, 0xcf, 0x56, 0xf9, 0x7b, 0xde, 0x7a, 0x3d, 0x2e, 0xb1,
    0x30, 0x3c, 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x07,
    0x30, 0x2f, 0xa0, 0x0f, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65,
    0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0xa1, 0x1c, 0x30, 0x1a, 0x06, 0x09, 0x2a,
    0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x08, 0x30, 0x0d, 0x06, 0x09, 0x60,
    0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x82, 0x01,
    0x00, 0xa0, 0x61, 0x45, 0x93, 0xa0, 0x30, 0xee, 0x33, 0x6b, 0xff, 0xf0, 0x76,
    0x61, 0x8b, 0x01, 0x30, 0xd0, 0x45, 0xde, 0x9f, 0x4d, 0x18, 0x3d, 0x83, 0xc5,
    0xda, 0x88, 0x2f, 0x13, 0xab, 0xc1, 0xf4, 0xbf, 0x0d, 0x13, 0x64, 0x58, 0x69,
    0x39, 0x34, 0x42, 0xab, 0x58, 0x2e, 0x6a, 0x04, 0xd6, 0x97, 0xd5, 0x2d, 0x56,
    0x23, 0xe3, 0x67, 0xe7, 0xe9, 0x2d, 0xb6, 0xeb, 0xd2, 0xbd, 0x07, 0x73, 0x5b,
    0x15, 0x3f, 0x28, 0x26, 0xbe, 0xe5, 0xdf, 0x8e, 0x16, 0xc7, 0x13, 0xa1, 0xe8,
    0x5c, 0xb6, 0xba, 0x85, 0xe8, 0x86, 0x81, 0x33, 0x5c, 0x7e, 0x94, 0x1a, 0x94,
    0xab, 0xf0, 0xc0, 0x11, 0x3e, 0x03, 0x09, 0x16, 0xe2, 0xb6, 0xe1, 0x82, 0x6d,
    0xb4, 0xff, 0xb9, 0x19, 0xb8, 0xe1, 0xb6, 0xf5, 0x9e, 0xc2, 0x77, 0xe5, 0x8d,
    0xe4, 0x0b, 0x0f, 0xfe, 0xec, 0xaa, 0x0d, 0x94, 0xfb, 0xcb, 0x29, 0x03, 0x74,
    0xed, 0x3d, 0xfc, 0x35, 0xd3, 0x1a, 0x8f, 0x43, 0xae, 0x13, 0x00, 0xfe, 0x20,
    0xfd, 0xd9, 0x86, 0xd4, 0xb8, 0x56, 0x56, 0x57, 0x45, 0x1f, 0x9d, 0xc6, 0x93,
    0xe1, 0x34, 0x67, 0x89, 0x20, 0x22, 0x1c, 0x58, 0xa2, 0x8d, 0xa5, 0xe7, 0x1a,
    0xb6, 0xb3, 0x1c, 0xaa, 0x44, 0x58, 0x25, 0xa6, 0x38, 0x5c, 0x6c, 0x4f, 0x5d,
    0x5d, 0x5e, 0xbb, 0xc2, 0xc2, 0x57, 0x0e, 0x96, 0x47, 0xfd, 0xed, 0x36, 0xb5,
    0x59, 0x64, 0x7b, 0xcb, 0x27, 0xe5, 0x88, 0x86, 0x25, 0x5f, 0x58, 0x2c, 0x63,
    0xd8, 0x63, 0xec, 0xec, 0x30, 0x7d, 0x15, 0x76, 0x3a, 0xbc, 0xe0, 0xb1, 0xb8,
    0x12, 0xa5, 0x50, 0x93, 0x8c, 0xa4, 0xe2, 0xd2, 0x8a, 0x74, 0x56, 0x3d, 0xb5,
    0x23, 0x67, 0x55, 0x8d, 0x3e, 0x2e, 0x82, 0xac, 0x2a, 0x27, 0xfe, 0x5c, 0x93,
    0x91, 0xa0, 0x07, 0xc1, 0x5f, 0x75, 0xb9, 0x0a, 0xb0, 0xdd, 0x30, 0x80, 0x06,
    0x09, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0x30, 0x1d, 0x06,
    0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a, 0x04, 0x10, 0x68,
    0x8c, 0x6d, 0x5f, 0x7f, 0x31, 0xb0, 0xfe, 0xce, 0xdf, 0x09, 0xf2, 0xe7, 0xc9,
    0x9a, 0xc9, 0xa0, 0x80, 0x04, 0x30, 0x70, 0xe5, 0xca, 0xaf, 0xfd, 0x49, 0xad,
    0x24, 0xef, 0xe1, 0x5b, 0xf9, 0x03, 0xbe, 0x9d, 0x19, 0x89, 0x5b, 0x77, 0x7d,
    0x26, 0x9b, 0x57, 0xb0, 0x25, 0xf6, 0xf6, 0x7e, 0x7e, 0xf9, 0x3f, 0x94, 0x46,
    0x45, 0x15, 0xf2, 0xeb, 0xe0, 0x34, 0xea, 0x0b, 0x76, 0x21, 0xa1, 0xff, 0x19,
    0x29, 0x2e, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};

static uint8_t cipher_text_der[] = {
    0x70, 0xe5, 0xca, 0xaf, 0xfd, 0x49, 0xad, 0x24, 0xef, 0xe1, 0x5b, 0xf9, 0x03,
    0xbe, 0x9d, 0x19, 0x89, 0x5b, 0x77, 0x7d, 0x26, 0x9b, 0x57, 0xb0, 0x25, 0xf6,
    0xf6, 0x7e, 0x7e, 0xf9, 0x3f, 0x94, 0x46, 0x45, 0x15, 0xf2, 0xeb, 0xe0, 0x34,
    0xea, 0x0b, 0x76, 0x21, 0xa1, 0xff, 0x19, 0x29, 0x2e};

static uint8_t iv_der[] = {
    0x68, 0x8c, 0x6d, 0x5f, 0x7f, 0x31, 0xb0, 0xfe, 0xce, 0xdf, 0x09, 0xf2, 0xe7,
    0xc9, 0x9a, 0xc9};

static uint8_t cipher_key_der[] = {
    0xa0, 0x61, 0x45, 0x93, 0xa0, 0x30, 0xee, 0x33, 0x6b, 0xff, 0xf0, 0x76, 0x61,
    0x8b, 0x01, 0x30, 0xd0, 0x45, 0xde, 0x9f, 0x4d, 0x18, 0x3d, 0x83, 0xc5, 0xda,
    0x88, 0x2f, 0x13, 0xab, 0xc1, 0xf4, 0xbf, 0x0d, 0x13, 0x64, 0x58, 0x69, 0x39,
    0x34, 0x42, 0xab, 0x58, 0x2e, 0x6a, 0x04, 0xd6, 0x97, 0xd5, 0x2d, 0x56, 0x23,
    0xe3, 0x67, 0xe7, 0xe9, 0x2d, 0xb6, 0xeb, 0xd2, 0xbd, 0x07, 0x73, 0x5b, 0x15,
    0x3f, 0x28, 0x26, 0xbe, 0xe5, 0xdf, 0x8e, 0x16, 0xc7, 0x13, 0xa1, 0xe8, 0x5c,
    0xb6, 0xba, 0x85, 0xe8, 0x86, 0x81, 0x33, 0x5c, 0x7e, 0x94, 0x1a, 0x94, 0xab,
    0xf0, 0xc0, 0x11, 0x3e, 0x03, 0x09, 0x16, 0xe2, 0xb6, 0xe1, 0x82, 0x6d, 0xb4,
    0xff, 0xb9, 0x19, 0xb8, 0xe1, 0xb6, 0xf5, 0x9e, 0xc2, 0x77, 0xe5, 0x8d, 0xe4,
    0x0b, 0x0f, 0xfe, 0xec, 0xaa, 0x0d, 0x94, 0xfb, 0xcb, 0x29, 0x03, 0x74, 0xed,
    0x3d, 0xfc, 0x35, 0xd3, 0x1a, 0x8f, 0x43, 0xae, 0x13, 0x00, 0xfe, 0x20, 0xfd,
    0xd9, 0x86, 0xd4, 0xb8, 0x56, 0x56, 0x57, 0x45, 0x1f, 0x9d, 0xc6, 0x93, 0xe1,
    0x34, 0x67, 0x89, 0x20, 0x22, 0x1c, 0x58, 0xa2, 0x8d, 0xa5, 0xe7, 0x1a, 0xb6,
    0xb3, 0x1c, 0xaa, 0x44, 0x58, 0x25, 0xa6, 0x38, 0x5c, 0x6c, 0x4f, 0x5d, 0x5d,
    0x5e, 0xbb, 0xc2, 0xc2, 0x57, 0x0e, 0x96, 0x47, 0xfd, 0xed, 0x36, 0xb5, 0x59,
    0x64, 0x7b, 0xcb, 0x27, 0xe5, 0x88, 0x86, 0x25, 0x5f, 0x58, 0x2c, 0x63, 0xd8,
    0x63, 0xec, 0xec, 0x30, 0x7d, 0x15, 0x76, 0x3a, 0xbc, 0xe0, 0xb1, 0xb8, 0x12,
    0xa5, 0x50, 0x93, 0x8c, 0xa4, 0xe2, 0xd2, 0x8a, 0x74, 0x56, 0x3d, 0xb5, 0x23,
    0x67, 0x55, 0x8d, 0x3e, 0x2e, 0x82, 0xac, 0x2a, 0x27, 0xfe, 0x5c, 0x93, 0x91,
    0xa0, 0x07, 0xc1, 0x5f, 0x75, 0xb9, 0x0a, 0xb0, 0xdd};

static void stream_buf_cleanup(struct stream_buf *buf)
{
    if (buf->buffer)
        free(buf->buffer);

    buf->buffer = NULL;
    buf->capacity = 0;
    buf->len = 0;
}

static void gtest_parse_enveloped_data(void)
{
    struct stream_buf respon_data;
    struct stream_buf cipher_symm_key = { 0 };
    struct stream_buf iv = { 0 };
    struct stream_buf cipher_text = { 0 };
    int rc;

    respon_data.capacity = respon_data.len = sizeof(enveloped_data_der);
    respon_data.buffer = enveloped_data_der;

    rc = parse_response_enveloped_data(&respon_data, &cipher_symm_key, &iv, &cipher_text);
    g_assert_cmpint(rc, ==, CMS_SUCCESS);

    g_assert_cmpmem(cipher_key_der, sizeof(cipher_key_der),
        cipher_symm_key.buffer, cipher_symm_key.len);

    g_assert_cmpmem(iv_der, sizeof(iv_der), iv.buffer, iv.len);

    g_assert_cmpmem(cipher_text_der, sizeof(cipher_text_der),
        cipher_text.buffer, cipher_text.len);

    stream_buf_cleanup(&cipher_symm_key);
    stream_buf_cleanup(&iv);
    stream_buf_cleanup(&cipher_text);
}

int main(int argc, char *argv[])
{
    g_test_init(&argc, &argv, NULL);

    g_test_add_func("/cms/parse_enveloped_data", gtest_parse_enveloped_data);

    return g_test_run();
}
